package vn.com.pavibook.daoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import vn.com.pavibook.dao.CustomerDao;
import vn.com.pavibook.domain.model.Customer;
import vn.com.pavibook.domain.model.SearchResult;
import vn.com.pavibook.utils.DaoHelper;

@Repository
public class CustomerDaoImpl extends DaoHelper<Customer> implements CustomerDao {

	@Override
	public void createNewCustomer(Customer customer) {
		StringBuilder sql = new StringBuilder();
		sql.append("INSERT INTO customer (firstname, lastname, middle, email, active, birthday, ");
		sql.append(" gender, address, phone, point_cart, account_id, country) VALUES ");
		sql.append(" (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
		
		Object[] params = new Object[]{
				customer.getFirstname(), customer.getLastname(), customer.getMiddle(),
				customer.getEmail(), customer.getActive(), customer.getBirthday(),
				customer.getGender(), customer.getAddress(), customer.getPhone(),
				customer.getPointCart(), customer.getAccountId(), customer.getCountry()
		};
		insert(sql.toString(), params);
	}
	

	@SuppressWarnings("rawtypes")
	class CustomerRowMapper implements RowMapper {
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			Customer customer = new Customer();
			customer.setId(rs.getInt("id"));
			customer.setFirstname(rs.getString("firstname"));
			customer.setLastname(rs.getString("lastname"));
			customer.setMiddle(rs.getString("middle"));
			customer.setEmail(rs.getString("email"));
			customer.setActive(rs.getBoolean("active"));
			customer.setBirthday(rs.getDate("birthday"));
			customer.setGender(rs.getString("gender"));
			customer.setAddress(rs.getString("address"));
			customer.setPhone(rs.getString("phone"));
			customer.setPointCart(rs.getDouble("point_cart"));
			customer.setAccountId(rs.getInt("account_id"));
			customer.setCountry(rs.getString("country"));
			return customer;
		}
	}


	@SuppressWarnings("unchecked")
	@Override
	public SearchResult<Customer> getCustomerByUsername(String username) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT * FROM Customer C left join account AC on C.account_id = AC.id WHERE AC.username = ?");
		Object[] param = new Object[] { username };
		return query(sql.toString(), param, new CustomerRowMapper());
	}


	@Override
	public void updateCustomerProfile(Customer customer) {
		StringBuilder sql = new StringBuilder();
		sql.append("UPDATE Customer set gender = ?, birthday=?, address = ?, phone = ?, email=?, country=? ");
		sql.append(" WHERE id = ?");
		Object[] params = new Object[]{
				customer.getGender(), customer.getBirthday(), customer.getAddress(), 
				customer.getPhone(), customer.getEmail(), customer.getCountry(), customer.getId()
		};
		insert(sql.toString(), params);
		
	}
	

	@Override
	public int getCustomerIdByUsername(String username) {
		String sql = "SELECT m.id FROM customer m inner join account a on m.account_id = a.id WHERE a.username = ?";
		Object[] params = new Object[]{username};
		return getJdbcTemplate().queryForInt(sql, params);
	}


	@Override
	public void updateAmountOfCustomer(Customer customer){
		StringBuilder sql = new StringBuilder();
		sql.append("UPDATE Customer set point_cart = ?");
		sql.append(" WHERE id = ?");
		Object[] params = new Object[]{customer.getPointCart(), customer.getId()};
		insert(sql.toString(), params);
	}

}
